/**
*
* \file    iNemo.c
* \author  ART Team IMS-Systems Lab
* \version V1.0
* \date    June , 2010
* \brief   This file define iNEMO Task.
*
*
* \details
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
* <h2><center>&copy; COPYRIGHT 2009 STMicroelectronics</center></h2>
*/


#include"iNemo.h"
#include "stm32f10x.h"
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"
#include "GUI_layer.h"
#include "utils.h"
#include "iNEMO_lib.h"

extern u32 count_out;
extern u8 buffer_rx[VIRTUAL_COM_PORT_DATA_SIZE];

/**
 * @ addtogroup iNEMO_task
 * @{
 */

/**
 * This resource is used to synchronize the USB IRQ and the iNemo Command Manager task.
 */
static xQueueHandle s_usbQueue;




/* Forward declaration */
static void inCommandTaskFunction(void *pvParameters);




void inInitTask(void *pvParameters)
{
	iNEMO_HW_Config();

	//while(bDeviceState != CONFIGURED);

	iNEMO_Config();

}

void inStartTask(void *pvParameters)
{
	// Create the queue used to synchronize iNemo Command Manager task end USB interrupt service routine.
	s_usbQueue = xQueueCreate(1, sizeof(u32));
	if (!s_usbQueue) {
		// Error in resource creation.
		while (1);
	}

	// Create the iNemo Command task
	if ( xTaskCreate(inCommandTaskFunction, "iNemoCmd", IN_TASK_STACK_SIZE, NULL, IN_TASK_PRIORITY+1, NULL) != pdPASS ) {
		// Error in task creation
		while (1);
	}

	// Create the iNemo Data stream task
}

void inCommandTaskFunction(void *pvParameters) {
	u32 _count_out = 0;

	for (;;) {
		if ( xQueueReceive(s_usbQueue, &_count_out, portMAX_DELAY ) == pdTRUE ) {
			ParseGuiFrame(_count_out);
		}
	}
}

void USB_LP_CAN1_RX0_IRQHandler(void)
{
#ifdef _VCOM
	portBASE_TYPE xHigherPriorityTaskWoken = pdFALSE;

	USB_Istr();
	if (count_out) {
		xQueueSendFromISR(s_usbQueue, &count_out, &xHigherPriorityTaskWoken);
		count_out=0;
	}
	portEND_SWITCHING_ISR(xHigherPriorityTaskWoken);
#endif
}


